<?php
    include './public/db_object.php';

    if(isset($_POST)){
        $name = $_POST['name'];
        $pass = $_POST['pass'];
        $phone = $_POST['phone'];
        $email = $_POST['email'];

        $db = new DB_object(['db_name'=>'db_shop']);


        $select_sql = "select * from user where name='".$name."'";
        $select_phone = "select * from user where phone='".$phone."'";
        $select_email = "select * from user where phone='".$email."'";
        $select_res = $db->l_select_one($select_sql);
        $select_phone_res = $db->l_select_one($select_phone);
        $select_email_res = $db->l_select_one($select_email);
        if(!empty($select_res)){
            echo 2; // 该用户名已存在
            exit;
        }

        if(!empty($select_phone_res)){
            echo 3;
            exit;
        }

        if(!empty($select_email_res)){
            echo 4;
            exit;
        }
   

        // 获取salt盐
        $str = "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM";
        for($i=0; $i<26; $i++){
            @$salt .= substr(str_shuffle($str), mt_rand(0, strlen($str)-1), 1);
        }
        // 加密
        $new_pass = md5($pass.$salt);

        $arr_data = ["null", "'$name'", "'$new_pass'", "'$phone'", "'$email'", "now()", "'男'", 'null', "'$salt'"];

        $res = $db->l_insert_one('user', $arr_data);
        
        if($res){
            echo 1;
        }
        else{
            echo 0;
        }
    }

?>